﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MusicLibrary.DataModels;
using MusicLibrary.Web;
using MusicLibrary.Data;
using MusicLibrary.Web.IDataAdapters.Interfaces;
using MusicLibrary.Web.Models;

namespace MusicLibrary.Web.IDataAdapters.Adapters
{
    public class MusicLibraryAdapter : IMusicLibraryAdapter
    {
        public List<Models.AlbumListVM> GetAlbumList()
        {
            List<AlbumListVM> albums = new List<AlbumListVM>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                albums = db.Albums.Select(x=> new AlbumListVM(){
                    Id = x.Id,
                    Title = x.Title,
                    Artist = x.Artist,
                    Image = x.Image,
                    Description = x.Description
                }).ToList();
            }
            return albums;
        }


        public List<SongListVM> GetSongList(int id)
        {
            List<SongListVM> songs = new List<SongListVM>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                songs = db.Songs.Where(x=>x.AlbumId == id).Select(x => new SongListVM()
                {
                    Id = x.Id,
                    Title = x.Title,
                    FeaturedArtist = x.FeaturedArtist,
                    Genre = x.Genre,
                    YoutubeUrl = x.YoutubeUrl,
                    AlbumId = x.AlbumId
                }).ToList();
            }
            return songs;
        }

        public Album AddAlbum(Album album)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                album = db.Albums.Add(album);
                db.SaveChanges();
            }
            return album;
        }
    }
}